#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Copyright © 启明星辰 版权所有
# @Time    : 2023/11/6 22:32
# @Author  : 胡浩浩
# @File    : count_data_api.py
# @IDE     : PyCharm
# @description :
from apps.models.count_data_model import CountData, CountDataModel

from fastapi import APIRouter

count_data_router = APIRouter()


@count_data_router.get("/count_data")
async def get_count_data():
    count_data = await CountDataModel.all()
    count_data_list = [[cd.product_name, cd.product_version, cd.cover_rate, cd.all_api_list, cd.case_api_list] for cd in
                       count_data]
    #
    return count_data_list


@count_data_router.get("/product_info")
async def get_product_info():
    product_info = await CountDataModel.all()
    product_info_list = [[cd.product_name + ":" + cd.product_version] for cd in
                         product_info]
    return product_info_list


@count_data_router.post("/count_data")
async def insert_count_data(countdata: CountData):
    print(countdata.product_name)
    select_count_data = await CountDataModel.filter(product_name=countdata.product_name,
                                                    product_version=countdata.product_version).first()
    if select_count_data:
        select_count_data.product_name = countdata.product_name
        select_count_data.product_version = countdata.product_version
        select_count_data.cover_rate = countdata.cover_rate
        select_count_data.all_api_list = countdata.all_api_list
        select_count_data.case_api_list = countdata.case_api_list
        await select_count_data.save(
            update_fields=["product_name", "product_version", "cover_rate", "all_api_list", "case_api_list"])
        # print(select_count_data.product_name)
        return {"message": f"产品{countdata.product_name}对应的{countdata.product_version}版本的接口统计数据更新完成"}
    else:
        select_count_data = CountDataModel(product_name=countdata.product_name,
                                           product_version=countdata.product_version,
                                           cover_rate=countdata.cover_rate, all_api_list=countdata.all_api_list,
                                           case_api_list=countdata.case_api_list)
        await select_count_data.save()
        return {"message": f"产品{countdata.product_name}接口统计数据新增成功", "data": countdata}
